package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.itheima.entity.Result;
import com.itheima.pojo.Tag;
import com.itheima.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.Tags;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 博客标签相关接口
 */

@CrossOrigin
@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * 增加标签
     * @param tagVO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Map<String, Object> tagVO){
        //传入数据为null的判断
        if(tagVO == null){
            return new Result<>("error","未传入数据");
        }
        try{
            tagService.add(tagVO);
            return new Result<>("success","插入成功");
        } catch (Exception e){
            e.printStackTrace();
            return new Result<>("error","插入失败");
        }
    }

    /**
     * 批量删除标签
     * @param tagVoList
     * @return
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Map<String,Object>> tagVoList){
        Map<String,Object> map = new HashMap<>();
        try{
            tagService.deleteBatch(tagVoList);
            map.put("success",1);
            map.put("resultCode","00002");
            map.put("message","操作成功！");
            return new Result<>("success",map);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success",0);
            map.put("resultCode","00001");
            map.put("message","操作失败！");
            return new Result<>("fail",map);
        }
    }

    /**
     * 编辑标签
     * @param tagVO
     * @return
     */
    @PostMapping("/edit")
    public String edit(@RequestBody Map<String,Object> tagVO){
        if(tagVO == null){
            return JSON.toJSONString(new Result<>("error","参数传入错误"));
        }
        Map<String,Object> map = new HashMap<>();
        try{
            tagService.edit(tagVO);
            return JSON.toJSONString(new Result<>("success","更新成功！"));
        }catch (RuntimeException e){
            e.printStackTrace();
            map.put("success",0);
            map.put("resultCode","00001");
            map.put("message",e.getMessage());
            return JSON.toJSONString(map);
        } catch (Exception e) {
            return JSON.toJSONString(new Result<>("error","更新失败！"));
        }
    }

    /**
     * 分页获取标签列表
     * @param tagVO
     * @return
     */
    @PostMapping("/getList")
    public String getList(@RequestBody Map<String,Object> tagVO){
        if(tagVO == null){
            return JSON.toJSONString(new Result<>("error","参数传入有误"));
        }
        try{
            Page<Tag> tags = tagService.getList(tagVO);
            Map<String,Object> map = new HashMap<>();
            map.put("records",tags.getResult());
            map.put("total",tags.getTotal());
            map.put("size",tagVO.get("pageSize"));
            map.put("current",tagVO.get("currentPage"));
            map.put("orders",new int[0]);
            map.put("optimizeCountSql",true);
            map.put("isSearchCount",true);

            return JSON.toJSONString(new Result<>("success",map));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new Result<>("error","获取标签列表失败"));
        }

    }


    /**
     * 根据uid置顶标签
     * @param tagVO
     * @return
     */

    @PostMapping("/stick")
    public Result stick(@RequestBody Map<String, Object> tagVO) {
        if(tagVO == null){
            return new Result<>("error","参数列表错误！");
        }
        try{
            tagService.stick(tagVO);
            return new Result<>("success","操作成功");
        } catch (RuntimeException e){
            e.printStackTrace();
            return new Result<>("error",e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("error","操作失败");
        }
    }

    /**
     * 通过引用量排序标签
     * @return
     */
    @PostMapping("/tagSortByCite")
    public Result tagSortByCite(){
        List<Map<String,Object>> tags = tagService.tagSortByCite();
        return new Result<>("success","操作成功");

    }

    /**
     * 通过点击量排序标签
     * @return
     */
    @PostMapping("/tagSortByClickCount")
    public Result tagSortByClickCount(){
        try{
            List<Tag> tags = tagService.tagSortByClickCount();
            return new Result("success","操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","操作失败");
        }
    }

}
